import {useState} from 'react';

// 员工修改密码

interface UseModifyPassword {
    modifyPassword: (password: string, newPassword: string) => Promise<void>;
    isLoading1: boolean;
    error1: string | null;
}

export function useModifyPassword(): UseModifyPassword {
    const [isLoading1, setisLoading1] = useState(false);
    const [error1, setError1] = useState<string | null>(null);

    const token = localStorage.getItem('token');
    const modifyPassword = async (password: string, newPassword: string): Promise<void> => {
        setisLoading1(true);
        setError1(null);
        try {
            if (!token) {
                setError1('未提供授权令牌');
                return;
            }
            const response = await fetch('http://localhost:6868/employee/modifyPassword', {
                method: 'POST',
                headers: {
                    'authorization': token!,
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify({password, new_password: newPassword}),
            });

            if (!response.ok) {
                throw new Error('修改密码失败');
            }

        } catch (error1) {
            setError1((error1 as Error).message);
        } finally {
            setisLoading1(false);
        }
    };

    return {modifyPassword, isLoading1, error1};
}
